package com.jason.quectel0805.net;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author：Jason
 * @date：2022/8/10 14:50
 * @email：1129847330@qq.com
 * @description:
 */
public class IOUtils {
    private static final String TAG = "jasonwan";

    public static boolean writeData2PrivateDir(String content, String filePath) {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);
            fileWriter.write(content);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "writeData2PrivateDir: \n" + e.getMessage());
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    public static String byteArray2File(byte[] bytes) {
        File rootFile = Environment.getExternalStorageDirectory();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");
        String fileName = simpleDateFormat.format(new Date()) + System.currentTimeMillis() + ".jpg";
        File file = new File(rootFile, fileName);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(bytes);
            return file.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }
}